package com.bootdo.cm.controller;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.system.service.DeptService;
import com.bootdo.util.IdWorker;
import com.bootdo.util.SnowflakeIdWorkerUtil;
import com.bootdo.util.UserUtil;
import org.apache.poi.ss.usermodel.Workbook;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.bootdo.common.utils.text.StringUtils;
import com.bootdo.system.domain.DeptDO;
import org.apache.poi.ss.usermodel.Workbook;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bootdo.common.utils.DateUtils;

import com.bootdo.cm.domain.SmsAcceptMsgDO;
import com.bootdo.cm.service.SmsAcceptMsgService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.FileUtil;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 短信接收记录表
 *
 * @author admin
 * @email ${email}
 * @date 2024-03-29
 */

@Controller
@RequestMapping("/cm/smsAcceptMsg")
public class SmsAcceptMsgController {
    @Autowired
    private SmsAcceptMsgService smsAcceptMsgService;

    @Autowired
    private BootdoConfig bootdoConfig;

    @Autowired
    private DeptService deptService;

    /**
     * 导入短信接收记录表
     *
     * @param file
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/importSmsAcceptMsgData")
//    @RequiresPermissions("cm:smsAcceptMsg:importColumn")
    R importSmsAcceptMsgData(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String fileName = file.getOriginalFilename();
        String fileNameOriginal = fileName;
        fileName = FileUtil.renameToUUID(fileName);
        String date = com.bootdo.util.DateUtils.format(new Date());
        try {
            // 上传文件
            FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath() + date + "/", fileName);
//                if (!fileNameOriginal.contains("桥梁媒体导入模板")) {
//                    return R.error("文件模板名称错误，请下载模板!");
//                }
            List<SmsAcceptMsgDO> smsAcceptMsgDOS = importExcel(bootdoConfig.getUploadPath() + date + "/" + fileName, 1, 1, SmsAcceptMsgDO.class);
            return importSmsAcceptMsg(smsAcceptMsgDOS);
        } catch (Exception e) {
            return R.error();
        }
    }


    public <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        } catch (Exception e) {
            e.printStackTrace();

        }
        return list;
    }


    /**
     * 导入短信接收记录表数据
     *
     * @param smsAcceptMsgDOS
     * @return
     */
    private R importSmsAcceptMsg(
    List<SmsAcceptMsgDO> smsAcceptMsgDOS)

    {
//        if (smsAcceptMsgDOS.size() > 0) {
//            for (SmsAcceptMsgDO smsAcceptMsgDO : smsAcceptMsgDOS) {
//                if (StringUtils.isNotBlank(smsAcceptMsgDO.getDepartment())) {
//                    SnowflakeIdWorkerUtil idWorker = new SnowflakeIdWorkerUtil(1, 1);
//                        smsAcceptMsgDO.setMediaId(idWorker.nextId() + "");
//                        smsAcceptMsgDO.setCreateDatetime(new Date());
//                        smsAcceptMsgDO.setUpdateDatetime(new Date());
//                        smsAcceptMsgDO.setDelFlag("0");
//                        DeptDO deptDO = deptService.get(UserUtil.getUserDO().getDeptId());
//                        smsAcceptMsgDO.setFillDepart(deptDO.getName());
//                        smsAcceptMsgDO.setFillDepartId(deptDO.getDeptId().toString());
//                        smsAcceptMsgDO.setFillUser(UserUtil.getUserDO().getUsername());
//                        smsAcceptMsgDO.setFillUserId(UserUtil.getUserDO().getUserId().toString());
//                        smsAcceptMsgDO.setSpare1("未使用");
//                        smsAcceptMsgService.save(smsAcceptMsgDO);
//                }
//            }
//        }
        return R.ok();
    }

    /**
     * 下载短信接收记录表
     */
    @RequestMapping("/downLoadSmsAcceptMsgData")
    @ResponseBody
//    @RequiresPermissions("cm:smsAcceptMsg:importColumn")
    void downLoadSmsAcceptMsgData(String type, HttpServletResponse response, @RequestParam Map<String, Object> params) throws Exception {
        List<SmsAcceptMsgDO> smsAcceptMsgDOS = smsAcceptMsgService.list(params);
        List<SmsAcceptMsgDO> smsAcceptMsgDOS1 = new ArrayList<>();
        if (smsAcceptMsgDOS.size() > 0) {
                smsAcceptMsgDOS1.add(smsAcceptMsgDOS.get(0));
        }
        String title = "短信接收记录表模板";
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, null),
            SmsAcceptMsgDO.class, smsAcceptMsgDOS1);
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(title, "utf-8") + ".xls");
        workbook.write(response.getOutputStream());
        workbook.close();
    }


    /**
    * 导出
    */
    @RequestMapping("/exportColumn")
    @ResponseBody
//    @RequiresPermissions("cm:smsAcceptMsg:exportColumn")
    void exportColumn(String type, HttpServletResponse response, @RequestParam Map<String, Object> params) throws Exception {
        List<SmsAcceptMsgDO> smsAcceptMsgDOS = smsAcceptMsgService.list(params);
        String title = "短信接收记录表" + DateUtils.format(new Date());
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, null),
            SmsAcceptMsgDO.class, smsAcceptMsgDOS);
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(title, "utf-8") + ".xls");
        workbook.write(response.getOutputStream());
        workbook.close();
    }

    @GetMapping()
//    @RequiresPermissions("cm:smsAcceptMsg:smsAcceptMsg")
    String SmsAcceptMsg() {
        return "cm/smsAcceptMsg/smsAcceptMsg" ;
    }

    @ResponseBody
    @GetMapping("/list")
//    @RequiresPermissions("cm:smsAcceptMsg:smsAcceptMsg")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<SmsAcceptMsgDO> smsAcceptMsgList = smsAcceptMsgService.list(query);
        int total = smsAcceptMsgService.count(query);
        PageUtils pageUtils = new PageUtils(smsAcceptMsgList, total);
        return pageUtils;
    }

    @GetMapping("/add")
//    @RequiresPermissions("cm:smsAcceptMsg:add")
    String add() {
        return "cm/smsAcceptMsg/add" ;
    }

    @GetMapping("/edit/{id}")
//    @RequiresPermissions("cm:smsAcceptMsg:edit")
    String edit(@PathVariable("id") Long id, Model model) {
            SmsAcceptMsgDO smsAcceptMsg = smsAcceptMsgService.get(id);
        model.addAttribute("smsAcceptMsg", smsAcceptMsg);
        return "cm/smsAcceptMsg/edit" ;
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
//    @RequiresPermissions("cm:smsAcceptMsg:add")
    public R save( SmsAcceptMsgDO smsAcceptMsg) {
        IdWorker idWorker = new IdWorker();
        // smsAcceptMsg.setUuid(idWorker.nextId() + "");
        if (smsAcceptMsgService.save(smsAcceptMsg) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
//    @RequiresPermissions("cm:smsAcceptMsg:edit")
    public R update( SmsAcceptMsgDO smsAcceptMsg) {
            smsAcceptMsgService.update(smsAcceptMsg);
        return R.ok();
    }

    @GetMapping("/review/{id}")
//    @RequiresPermissions("cm:smsAcceptMsg:review")
    String review(@PathVariable("id") Long id, Model model) {
            SmsAcceptMsgDO smsAcceptMsg = smsAcceptMsgService.get(id);
        model.addAttribute("smsAcceptMsg", smsAcceptMsg);
        return "cm/smsAcceptMsg/review" ;
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
//    @RequiresPermissions("cm:smsAcceptMsg:remove")
    public R remove( Long id) {
        if (smsAcceptMsgService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
//    @RequiresPermissions("cm:smsAcceptMsg:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids) {
            smsAcceptMsgService.batchRemove(ids);
        return R.ok();
    }

}
